package com.wei.controller;

import com.wei.entity.Subject;
import com.wei.service.SubjectService;
import com.wei.service_base.exception.XueYuanException;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author wjx
 * @ClassName SubjectController
 * @description: TODO
 * @date 2022-11-29 16:44
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduservice/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 添加课程分类
     * 获取上传过来的文件，把文件内容读取出来
     *
     * @param file
     * @return
     */
    @PostMapping("/addSubject")
    public R addSubject(MultipartFile file) {

        if (file == null) {
            throw new XueYuanException(20001, "文件不能为空");
        } else {

            //subjectExample.xlsx
            String fileName = file.getOriginalFilename();

            //.xlsx
            String fileType = fileName.substring(fileName.lastIndexOf("."));

            if (".xls".equalsIgnoreCase(fileType) || ".xlsx".equalsIgnoreCase(fileType)) {
                subjectService.saveSubject(file);

                return R.ok();
            } else {
                throw new XueYuanException(20001, "类型不匹配");
            }
        }
    }

    @GetMapping("/list")
    public R showSubject() {

        List<Subject> list = subjectService.getAllSubject();

        return R.ok().data("list", list);
    }
}
